const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['1', '2', '3', '4', '5', '6', '7', '8', '9'],
    index: '',
    checkImages: null,
    checkid:""
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  initImages: function (num) {
    var result = [];
    for (var i = 0; i < num; i++) {
      var img = {
        filePath: "",
        hasImage: false,
        index: i,
        imageUrl: ""
      }
      result.push(img);
    }
    this.setData({ checkImages: result });
  },
  chooseImageUi: function (event) {
    var that = this;
    var num = event.currentTarget.dataset.index;
    var currentImage = this.data.checkImages[num];
    wx.chooseImage({
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album','camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片

        
        that.uploadImage(res.tempFilePaths[0], function (data) {
          currentImage.filePath = data.url;
          currentImage.hasImage = true;
          currentImage.imageUrl = data.url;
          that.data.checkImages[num] = currentImage;
          that.setData({ checkImages: that.data.checkImages });
        })
      }
    })
  },
  removeimage: function (event) {
    var num = event.currentTarget.dataset.index;
    var currentImage = this.data.checkImages[num];
    currentImage.hasImage = false;
    currentImage.filePath = "";
    currentImage.imageUrl = "";
    this.data.checkImages[num] = currentImage;
    this.setData({ checkImages: this.data.checkImages });
  },
  uploadImage: function (filePath, callback) {
    wx.showLoading({
      title: '上传图片中'
    });
    wx.uploadFile({
      url: app.globalData.serverPath + 'api/upload-pic-two',
      filePath: filePath,
      name: 'myfile',
      formData: {
        'lat': app.globalData.latitude,
        'lng': app.globalData.longitude
      },
      success: function (res) {
        var data = res.data
        var urldata = JSON.parse(res.data);
        if (urldata && urldata.url != "null") {
          if (callback) {
            callback(urldata);
          }
        } else {
          wx.showModal({
            title: '错误提示',
            content: '照片上传失败，请重试',
            showCancel: false
          })
        }
      },
      complete: function () {
        wx.hideLoading();
      }
    })
  },


  submit: function (e) {
    var pages = getCurrentPages();
    var images = [];
    for (var i = 0; i < this.data.checkImages.length; i++) {
      if (this.data.checkImages[i].imageUrl.length > 0) {
        images.push(this.data.checkImages[i].imageUrl);
      }
    }
    if (e.detail.value.rectItem==""){
      wx.showModal({
        title: '错误提示',
        content: '请填写整改结果',
        showCancel:false
      })
      return;
    }

    if (images.length==0) {
      wx.showModal({
        title: '错误提示',
        content: '请上传整改结果照片',
        showCancel: false
      })
      return;
    }
    wx.request({
      method: 'POST',
      url: app.globalData.serverPath + "api/bs-car-safe-checks-save-result?checkid=" + this.data.checkid + "&result=" + encodeURIComponent(e.detail.value.rectItem),
      header: {
        'Authorization': app.globalData.token
      },
      data: images,
      success: function (res) {
        if (res.statusCode == 200) {
          wx.navigateBack({
            delta:1
          });
        }
      },
      complete: function () {
        wx.hideLoading();
      }

    });
    console.log("zhenggai result", pages, pages[0]);
    wx.navigateBack();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.initImages(6);
    this.setData({checkid:options.checkid});
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})